package ex.com.jm.food.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import ex.com.jm.food.entity.Company;
import ex.com.jm.food.service.AdminCompanyService;
import ex.com.jm.food.utils.DateUtils;
import ex.com.jm.food.utils.JsonUtils;
import ex.com.jm.food.utils.MD5Utils;

public class AdminCompanyAction extends ActionSupport {

	private static final long serialVersionUID = 1L;
	
	private AdminCompanyService adminCompanyService;
	private Company company;
	private int initPage=1;
	private int pageRow=10;
	private int totalCount;
	private String comName;
	private Integer status;
	private String s_date;
	private String e_date;
	private String comId;
	private String comPower;
	private String sort;
	private String order;
	private String province;
	private String citys;
	private String countys;
	private String streets;
	private String comEmail;
	private String card;
	private Map<String, String> dateMap;
	public AdminCompanyService getAdminCompanyService() {
		return adminCompanyService;
	}
	public void setAdminCompanyService(AdminCompanyService adminCompanyService) {
		this.adminCompanyService = adminCompanyService;
	}
	public Company getCompany() {
		return company;
	}
	public void setCompany(Company company) {
		this.company = company;
	}
	public int getInitPage() {
		return initPage;
	}
	public void setInitPage(int initPage) {
		this.initPage = initPage;
	}
	public int getPageRow() {
		return pageRow;
	}
	public void setPageRow(int pageRow) {
		this.pageRow = pageRow;
	}
	public int getTotalCount() {
		return totalCount;
	}
	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}
	public String getComName() {
		return comName;
	}
	public void setComName(String comName) {
		this.comName = comName;
	}
	public Integer getStatus() {
		return status;
	}
	public void setStatus(Integer status) {
		this.status = status;
	}
	public String getS_date() {
		return s_date;
	}
	public void setS_date(String s_date) {
		this.s_date = s_date;
	}
	public String getE_date() {
		return e_date;
	}
	public void setE_date(String e_date) {
		this.e_date = e_date;
	}
	public String getComId() {
		return comId;
	}
	public void setComId(String comId) {
		this.comId = comId;
	}
	public String getComPower() {
		return comPower;
	}
	public void setComPower(String comPower) {
		this.comPower = comPower;
	}

	public String getSort() {
		return sort;
	}
	public void setSort(String sort) {
		this.sort = sort;
	}
	public String getOrder() {
		return order;
	}
	public void setOrder(String order) {
		this.order = order;
	}
	
	public String getProvince() {
		return province;
	}
	public void setProvince(String province) {
		this.province = province;
	}
    public String getCitys() {
		return citys;
	}
	public void setCitys(String citys) {
		this.citys = citys;
	}
	public String getCountys() {
		return countys;
	}
	public void setCountys(String countys) {
		this.countys = countys;
	}
	public String getStreets() {
		return streets;
	}
	public void setStreets(String streets) {
		this.streets = streets;
	}
	public String getComEmail() {
		return comEmail;
	}
	public void setComEmail(String comEmail) {
		this.comEmail = comEmail;
	}
	public String getCard() {
		return card;
	}
	public void setCard(String card) {
		this.card = card;
	}
	public Map<String, String> getDateMap() {
		return dateMap;
	}
	public void setDateMap(Map<String, String> dateMap) {
		this.dateMap = dateMap;
	}
	/**
	 * 到达列表页面
	 * @return
	 */
	public String comList(){
		dateMap=DateUtils.getDate();
		return "comList";
	}
	
	/**
	 * 获取数据，ajax返回，处理json
	 */
	public void getComs(){
		HttpServletResponse response = ServletActionContext.getResponse();  
		response.setContentType("text/plain;charset=utf-8");
		int offset=(initPage-1)*pageRow;
		List<Company> companies=this.getAdminCompanyService().getCompanies(offset, pageRow, comName, status, s_date, e_date, sort, order);
		JSONArray jsoncom=JSONArray.fromObject(companies);
		totalCount=this.getAdminCompanyService().companyCount(comName, status, s_date, e_date);
		JSONObject jo=JsonUtils.buildJson(jsoncom, initPage, pageRow, totalCount);
		PrintWriter out=null;
		try {
			out = response.getWriter();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(jo);
		out.print(jo);
		out.flush();
		out.close();
	}
	/**
	 * 获取企业信息/到达添加页面
	 * @return
	 */
	public String editCompany(){
		if(comId!=null){
			company=this.getAdminCompanyService().editCompany(comId);
			String[] address=company.getComAddress().split(";");
		    province=address[0];
		    citys=address[1];
		    countys=address[2];
		    streets=address[3];
		}
		return "edit";
		
	}
	/**
	 * 企业信息保存/更新
	 * @return
	 */
	public String saveCompany(){
		if(company.getComEmail()==null||company.getComEmail().equals("")){
			addFieldError("company.comEmail", "邮箱不能为空");
			return "edit";
		}else if (company.getComName()==null||company.getComName().equals("")) {
			addFieldError("company.comName", "名称不能为空");
			return "edit";
		}else if (company.getComPwd()==null||company.getComPwd().equals("")) {
			addFieldError("company.comPwd", "密码不能为空");
			return "edit";
		}
		String address=province+";"+citys+";"+countys+";"+streets;
		if(address==";;;"){
			addFieldError("province", "地址不能为空");
			return "edit";
		}
		company.setComAddress(address);
		company.setComPwd(MD5Utils.GetMD5Code(company.getComPwd()));
		if(company.getComId()==null||company.getComId().equals("")){
			company.setCreated(DateUtils.getCurrentDate());
			company.setStatus(1);
		}
		Company checkName=this.getAdminCompanyService().checkName(comName);
		Company checkEmail=this.getAdminCompanyService().checkEmail(comEmail);
		if(checkName!=null&&company.getComId()==null){
			addFieldError("company.comName", "名称已被注册");
			return "edit";
		}
		if(checkEmail!=null&&company.getComId()==null){
			addFieldError("company.comEmail", "邮箱已被注册");
			return "edit";
		}
		if(company.getComId()!=null&&!company.getComId().equals("")){
			this.getAdminCompanyService().updateCompany(company);
			return "comList";
		}
		this.getAdminCompanyService().saveCompany(company);
		return "comList";
		
	}
	/**
	 * 更新企业帐号状态
	 * @return
	 */
	public String updateStatus(){
		HttpServletResponse response = ServletActionContext.getResponse();  
		response.setContentType("text/plain;charset=utf-8");
		boolean flag=this.getAdminCompanyService().updateStatus(status, comId);
		if(flag==true){
		return "comList";
		}
		PrintWriter out=null;
		try {
			out = response.getWriter();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		out.print("<script>alert('更新失败！');history.go(-2);window.location.reload(true);</script>");
		out.flush();
		out.close();
		return "comList";
	}
	/**
	 * 获取企业权限
	 * @return
	 */
	public String editPower(){
		if(comId!=null&&!comId.equals("")){
			comPower=this.getAdminCompanyService().getPower(comId);
		}
		return "editPower";
		
	}
	/**
	 * 更新企业权限
	 * @return
	 */
	public String updatePower(){
		this.getAdminCompanyService().updatePower(comPower, comId);
		return "comList";
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public String intoAccount(){
		company=this.getAdminCompanyService().intoAccount(comId, comName);
		ActionContext actionContext = ActionContext.getContext();
		Map session = actionContext.getSession();
		session.put("company", company);
		return "comIndex";
	}
	/**
	 * 验证名称是否重复
	 */
	public void checkName(){
		Company checkName=this.getAdminCompanyService().checkName(comName);
		String msg="";
		if(checkName!=null&&comId==null){
			msg="该企业名称已被注册";
		}else{
			msg="企业名称可用！";
		}
		JSONArray jo=JsonUtils.buildMessage(msg);
		HttpServletResponse response = ServletActionContext.getResponse();  
		response.setContentType("text/plain;charset=utf-8");
		PrintWriter out=null;
		try {
			out = response.getWriter();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		out.print(jo);
	}
    /**
     * 验证企业邮箱
     */
	public void checkEmail(){
		Company checkEmail=this.getAdminCompanyService().checkEmail(comEmail);
		String msg="";
		if(checkEmail!=null&&comId==null){
			msg="该企业邮箱已被注册";
		}else{
			msg="企业邮箱可用！";
		}
		JSONArray jo=JsonUtils.buildMessage(msg);
		HttpServletResponse response = ServletActionContext.getResponse();  
		response.setContentType("text/plain;charset=utf-8");
		PrintWriter out=null;
		try {
			out = response.getWriter();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		out.print(jo);
	}
	/**
	 * 验证银行卡号
	 */
	public void checkBankCard(){
		Company checkBankCard=this.getAdminCompanyService().checkBankCard(card);
		String msg="";
		if(checkBankCard!=null&&comId==null){
			msg="该银行卡号已被注册";
		}else{
			msg="银行卡号可用！";
		}
		JSONArray jo=JsonUtils.buildMessage(msg);
		HttpServletResponse response = ServletActionContext.getResponse();  
		response.setContentType("text/plain;charset=utf-8");
		PrintWriter out=null;
		try {
			out = response.getWriter();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		out.print(jo);
	}
}

